<?php

/**
 * Created by IntelliJ IDEA.
 * User: andyhuang
 * Date: 2018/3/18
 * Time: 15:56
 */
class ActionResult
{
    /**
     * 响应码
     */
    protected $code;

    /**
     * 响应消息
     */
    protected $message;

    /**
     * 响应对象
     */
    protected $value;
    /**
     * 构造方法.
     */
    public function __construct($code, $message, $value)
    {
        $this->code = $code;
        $this->message = $message;
        $this->value = $value;
    }

    public function getJsonBody(){
        $result = array(
            "code" => $this->code,
            "message" => $this->message,
            "value" => $this->value,
        );
        $jsonencode = json_encode($result,JSON_UNESCAPED_UNICODE);
        return $jsonencode;
    }

    /**************************************************************
     *
     *  使用特定function对数组中所有元素做处理
     *  @param  string  &$array     要处理的字符串
     *  @param  string  $function   要执行的函数
     *  @return boolean $apply_to_keys_also     是否也应用到key上
     *  @access public
     *
     *************************************************************/
    public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            die('possible deep recursion attack');
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else {
                $array[$key] = $function($value);
            }

            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }
}